package cs235.xml;

import java.io.StringWriter;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

public class XMLElement extends XMLNode implements Element {

    private Map<String, String> attributes;
    private String tagName;

    public XMLElement() {
        this(null);
    }

    public XMLElement(Node parent) {
        super(ELEMENT_NODE, parent);
        attributes = new LinkedHashMap<String, String>();
    }
    
    @Override
    public String getTagName() {
        if (tagName == null) {
            throw new IllegalStateException("Tag name has not been set");
        }
        return tagName;
    }

    @Override
    public void setTagName(String tagName) {
        if (tagName == null) {
            throw new IllegalArgumentException("Tag name cannot be null");
        }
        this.tagName = tagName;
    }

    @Override
    public String getAttributeValue(String name) {
        if (!attributes.containsKey(name)) {
            throw new IllegalArgumentException("There is no attribute " + name
                    + " for this element.");
        }
        return attributes.get(name);
    }

    @Override
    public int getAttributeCount() {
        return attributes.size();
    }

    @Override
    public String getAttributeName(int i) {
        if (i > attributes.size()) {
            throw new IllegalArgumentException("There are not " + i + " attribues.");
        } else if (i < 0) {
            throw new IllegalArgumentException("Cannot use negative values.");
        }
        return attributes.keySet().toArray(new String[attributes.size()])[i];
    }

    @Override
    public String getAttributeValue(int i) {
        if (i > attributes.size()) {
            throw new IllegalArgumentException("There are not " + i + " attribues.");
        } else if (i < 0) {
            throw new IllegalArgumentException("Cannot use negative values.");
        }
        return attributes.values().toArray(new String[attributes.size()])[i];
    }

    @Override
    public void setAttribute(String name, String value) {
        if (name == null) {
            throw new IllegalArgumentException("attribute name cannot be null");
        } else if (value == null) {
            throw new IllegalArgumentException("attribute value cannot be null");
        }
        attributes.put(name, value);
    }

    
    @Override 
    public String toString(){
        StringWriter writer = new StringWriter();
        writer.write("<"+getTagName());
        Iterator<String> itr = attributes.keySet().iterator();
        while (itr.hasNext()){
            String key = itr.next();
            writer.write(" ");
            writer.write(key);
            writer.write("=\"");
            writer.write(attributes.get(key));
            writer.write("\"");
        }
        writer.write(">");
        Node child = getFirstChild();
        while (child!=null){
            writer.write(child.toString());
            child = child.getNextSibling();
        }
        writer.write("</"+getTagName()+">");
        return writer.toString();
    }

    
}
